package com.example.gyt.controller;

import com.example.gyt.pojo.Commodity;
import com.example.gyt.pojo.Land;
import com.example.gyt.pojo.PageResult;
import com.example.gyt.service.LandService;
import com.example.gyt.service.UserService;
import com.example.gyt.util.DateUtil;
import com.example.gyt.util.ResultUtil;
import com.example.gyt.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class LandJsonController {

    @Autowired
    private LandService landService;

    //添加土地方法
    @ResponseBody
    @RequestMapping(value = "/member/addLand",method = RequestMethod.POST)
    public ResultUtil addLand(@Validated @RequestBody Land land, BindingResult result){
        try {
            //是否为空检验
            if(StringUtils.isEmpty(land.getLandownerId())){
                return ResultUtil.failure("土地拥有者id不能为空");
            }
            if(StringUtils.isEmpty(land.getLandCultivationStatus())){
                return ResultUtil.failure("土地耕种状态不能为空");
            }
            if(StringUtils.isEmpty(land.getLandName())){
                return ResultUtil.failure("土地名称不能为空");
            }
            if(StringUtils.isEmpty(land.getLandContact())){
                return ResultUtil.failure("土地联系人不能为空");
            }

            land.setId(UuidUtil.getUUID());

            //获取到当前时间
            land.setCreateTime(DateUtil.getCurrentTime());

            int count = landService.insertSelective(land);
            if (count == 0){
                return ResultUtil.success("添加土地失败");
            }

            return ResultUtil.success("添加土地信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }

    }

    //删除一块土地
    @ResponseBody
    @RequestMapping(value = "/member/deleteLand",method = RequestMethod.POST)
    public ResultUtil deleteLand(@Validated @RequestBody Land land, BindingResult result){
        try {

            if(StringUtils.isEmpty(land.getId())){
                return ResultUtil.failure("土地ID不能为空");
            }

            int count = landService.deleteByPrimaryKey(land.getId());
            if (count == 0){
                return ResultUtil.failure("删除土地失败");
            }

            return ResultUtil.success("删除土地成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //土地ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/queryLand",method = RequestMethod.POST)
    public ResultUtil queryLand(@Validated @RequestBody Land land, BindingResult result){
        try {

            if(StringUtils.isEmpty(land.getId())){
                return ResultUtil.failure("土地ID不能为空");
            }

            Land land1 =landService.selectByPrimaryKey(land.getId());
            if (land1 == null){
                return ResultUtil.failure("获取土地信息失败");
            }

            return ResultUtil.success(land1);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //修改土地信息
    @ResponseBody
    @RequestMapping(value = "/member/updateLand",method = RequestMethod.POST)
    public ResultUtil updateLand(@Validated @RequestBody Land land, BindingResult result){
        try {
            if(StringUtils.isEmpty(land.getId())){
                return ResultUtil.failure("土地ID不能为空");
            }

            //更新修改时间
            land.setUpdateTime(DateUtil.getCurrentTime());

            int count = landService.updateByPrimaryKeySelective(land);
            if (count == 0){
                return ResultUtil.failure("修改土地信息失败");
            }

            return ResultUtil.success("修改土地信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //集合于土地分页查询，所有土地查询，土地类型查询，土地名字模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allLandList",method = RequestMethod.POST)
    public ResultUtil allLandList(@Validated @RequestBody Land land, BindingResult result){
        try {

            List<Land> land1 = landService.getAllLandList(land);
            if (land1.size() == 0){
                return ResultUtil.failure("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = landService.queryCountLike(land);

            //加载实体将数据返回给用户
            PageResult pageResult = new PageResult();
            if (land.getPageResult() != null){
                pageResult.setCurrentPage(land.getPageResult().getCurrentPage());
                pageResult.setPageSize(land.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(land1);

            return ResultUtil.success(pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }
}
